<?php

namespace app\app\controller;

use app\BaseController;
use app\app\model\TodoModel;
use app\app\model\TodoTaskModel;

class Todo extends BaseController
{
    public function statistics()
    {
        $query = $this->request->param();
        $name = $query['name'];

        $receive = TodoTaskModel::alias('a')
            ->field('b.*')
            ->where(['a.receive_user' => $name, 'a.status' => 1, 'b.state' => 1])
            ->join('t_project_yj b', 'a.link_id = b.id')->count();

        $send = TodoModel::alias('a')
            ->where(['create_user' => $name, 'state' => 1])->count();

        $done = TodoTaskModel::alias('a')
            ->field('b.*')
            ->where(['a.receive_user' => $name, 'a.status' => 2, 'b.state' => 1])
            ->join('t_project_yj b', 'a.link_id = b.id')->count();

        $res['receive'] = $receive;
        $res['send'] = $send;
        $res['done'] = $done;
        $this->success('success', $res);
    }

    public function list()
    {
        $query = $this->request->param();

        $name = $query['name'];
        $type = empty($query['type']) ? 'receive' : $query['type'];
        $page_num = empty($query['pageNum']) ? 1 : $query['pageNum'];
        $page_size = empty($query['pageSize']) ? 10 : $query['pageSize'];

        if ($type === 'receive') {
            $list = TodoTaskModel::alias('a')
                ->field('b.*,a.status')
                ->where(['a.receive_user' => $name, 'a.status' => 1, 'b.state' => 1])
                ->join('t_project_yj b', 'a.link_id = b.id')
                ->order('a.create_time DESC');
        } else if ($type === 'send') {
            $list = TodoModel::alias('a')
                ->where(['create_user' => $name])
                ->where('state', 1)
                ->order('yj_date DESC');
        } else if ($type === 'done') {
            $list = TodoTaskModel::alias('a')
                ->field('b.*, a.id as task_id,a.oper_date,a.status')
                ->where(['a.receive_user' => $name, 'a.status' => 2, 'b.state' => 1])
                ->join('t_project_yj b', 'a.link_id = b.id')
                ->order('a.create_time DESC');
        }

        $total = $list->count();
        $list = $list->limit($page_size * ($page_num - 1), $page_size)->select();
        $index = $page_size * ($page_num - 1);
        $list = $list->toArray();
        foreach ($list as $key => $value) {
            $index = $index + 1;
            $data[$key]['code'] = $index;
        }

        $res['list'] = $list;
        $res['pageIndex'] = $page_num;
        $res['pageSize'] = $page_size;
        $res['rowTotal'] = $total;

        $this->success('success', $res);
    }

    public function info()
    {
        $query = $this->request->param();
        $id = $query['id'];
        $res = TodoModel::where(['id' => $id])->find();
        $this->success('success', $res);
    }

    public function delete()
    {
        $query = $this->request->param();
        $type = empty($query['type']) ? 'receive' : $query['type'];
        $id = $query['id'];
        if ($type === 'send') {
            $update['state'] = 3;
            TodoModel::where(['id' => $id])->update($update);
            $update_to['status'] = 2;
//            TodoTaskModel::where(['link_id' => $id])->delete($update);
        } else if ($type === 'done') {
            $update['status'] = 2;
            TodoTaskModel::where(['id' => $id])->delete($update);
        }
        $this->success('success');
    }

    public function receiveList()
    {

        $query = $this->request->param();
        $id = $query['id'];
        $list = TodoTaskModel::where(['link_id' => $id])->field('receive_user, id, status')->select();

        $total = $list->count();
        $list = $list->toArray();
        $index = 0;
        foreach ($list as $key => $value) {
            $index = $index + 1;
            $data[$key]['code'] = $index;
        }
        $res['list'] = $list;
        $res['rowTotal'] = $total;

        $this->success('success', $res);
    }

    public function add()
    {
        $query = $this->request->param();
        $todi_id = $this->uuid();
        $query['id'] = $todi_id;
        $query['yj_date'] = date('Y-m-d H:i:s', time());
        $query['status'] = '1';
        $todo_m = new TodoModel();
        $todo_m->save($query);

        $arr = $var = explode(",", $query['receive_user']);
        $todo_task_list = array();

        foreach ($arr as $key => $value) {
            $item['id'] = $this->uuid();
            $item['receive_user'] = $value;
            $item['link_id'] = $todi_id;
            $item['status'] = '1';
            $item['end_date'] = $query['end_date'];
            $item['end_date'] = $query['end_date'];
            $item['create_time'] = date('Y-m-d H:i:s', time());
            array_push($todo_task_list, $item);
        }
        $todo_task_m = new TodoTaskModel();
        $res = $todo_task_m->saveAll($todo_task_list, false);

        $this->success('success', $res);
    }

    public function update()
    {
        $query = $this->request->param();
        if (empty($query['link_id']) || empty($query['receive_user'])) {
            $this->error('未传必要参数');
        }
        $todo_task_model = new TodoTaskModel();
        $task = TodoTaskModel::where('link_id', $query['link_id'])
            ->where('receive_user', $query['receive_user'])
            ->find();
        $task['status'] = '2';
        $task['oper_date'] = date('Y-m-d H:i:s', time());
        $task->save();
        $this->success('success', $task);
    }


    public function back()
    {

        $query = $this->request->param();
        $id = $query['id'];

        $res = TodoModel::where(['id' => $id])->find();
        if ($res && $res['state'] == 1) {
            $differ = floor((time() - strtotime($res['yj_date'])));
            if ($differ > 180) {
                $this->error('success', '不可撤回');
            }
            $update_todo['state'] = 2;
            TodoModel::where(['id' => $id])->update($update_todo);

            $update['status'] = 4;
            TodoTaskModel::where(['link_id' => $id])->update($update);
            $this->success('success', '撤销成功');
        }
        $this->success('success', '该待办不存在');
    }
}
